library(foreign)

cand <- read.csv('prq_fig1.csv')

#craetes year counter for loop. This line MUST be done after the years ending in 2 are dropped
cand$yearID <- as.numeric(as.factor(cand$year))

#needed for for loop and to create arrays below
N <- max(cand$yearID)

N <- max(cand$yearID, na.rm = TRUE)

##############################
#CORRELATION BY ELECTION TYPE#
##############################

p <- subset(cand, pres_elec == 1)
m <- subset(cand, pres_elec == 0)
p$yearID <- as.numeric(as.factor(p$year))
m$yearID <- as.numeric(as.factor(m$year))

N1 <- max(p$yearID, na.rm = TRUE)
N2 <- max(m$yearID, na.rm = TRUE)
  
#create arrays for coefficients from for loop
cor_p <- array(NA, dim = c(N1,1))
cor_m <- array(NA, dim = c(N2,1))

#for loop to extract coefficients
for(i in 1:N1){
  cor_p[i]<- cor(p$dv[p$yearID == i], p$dpres[p$yearID == i], use ="complete.obs")
  
  }

for(i in 1:N2){
  cor_m[i]<- cor(m$dv[m$yearID == i], m$dpres[m$yearID == i], use ="complete.obs")
}

#create figure
par(mar = c(5, 4, 2, 2))
r <- 1:N1
r2 <- 1:N2
plot(r, cor_p, ylim = c(0,1), type ='n', yaxt='n', xaxt = 'n', ylab='', xlab='', main = '')
lines(r, cor_p, lwd = 3, col = 'grey')
lines(r2, cor_m, lwd  = 3, lty = 2)
legend('bottomleft', bty = 'n', legend = c('Presidential', 'Midterm'), lwd = c(3,3), col = c('grey', 'black'), lty = c(1,2))

lab <- seq(1840, 2016, 8)
r2 <- seq(1, 45, 2)
  
#plot axes
axis(side = 1, at = r, labels = FALSE ) 
text(r2-0.5, par("usr")[3]-0.025 , labels = lab, srt = 45, pos = 1, xpd = TRUE, cex = 0.8)
axis(side =2, at = seq(0,1,0.2), las = 2)

#add margin text
mtext("Presidential Election Year", side = 1, line = 2.75)
mtext("Correlation", side = 2, line = 2.75)


